package com.jiuyuan.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.jiuyuan.entity.vo.RespBean;
import com.jiuyuan.entity.vo.RespBeanEnum;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: hrms
 * @description:
 * @author: Feng
 * @create: 2023-02-15 13:12
 **/

@Component
public class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    // 未登录或token过期，输出信息
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        RespBean respBean = RespBean.error(RespBeanEnum.NOT_LOGIN);
        PrintWriter writer = response.getWriter();
        writer.write(new ObjectMapper().writeValueAsString(respBean));
        writer.flush();
        writer.close();
    }
}
